package org.shoukaiseki.supersaiyan.generator.util;

import org.shoukaiseki.common.utils.BeanUtils;
import org.shoukaiseki.common.utils.BooleanUtils;
import org.shoukaiseki.common.utils.StringUtils;
import org.shoukaiseki.supersaiyan.generator.domain.GenTable;
import org.shoukaiseki.supersaiyan.generator.domain.GenTableColumn;
import org.shoukaiseki.supersaiyan.generator.ocgt.constant.OmniCodeGenTableConstants;
import org.shoukaiseki.supersaiyan.generator.ocgt.util.SksTemplateUtils;

import java.util.Map;

/**
 * org.shoukaiseki.supersaiyan.generator.util.SksGenTableUtils
 * <br>
 *
 * @author shoukaiseki blog http://www.shoukaiseki.top/<br> E-mail jiang28555@Gmail.com<br>
 * @date 2022-10-15 14:28:51<br>
 */
public class SksGenTableUtils {


    public static final SksGenTableUtils wgtUtils=new SksGenTableUtils();

    public static String joinNullIsEmpty(String... strs){
        StringBuffer sb=new StringBuffer();
        for (String str : strs) {
            if (!StringUtils.isEmpty(str)) {
                sb.append(str);
            }
        }
       return sb.toString();
    }


    //如果为空则返回str内容,否则返回空字符
    public static String ifFalseReturn(Object val,String str){
        if(BooleanUtils.isTrue(val)){
            return StringUtils.NULLSTR;
        }
        return str;
    }

    //如果不为空则返回str内容,否则返回空字符
    public static String ifTrueReturn(Object val,String str){
        if(BooleanUtils.isTrue(val)){
            return str;
        }
        return StringUtils.NULLSTR;
    }

    //如果不为空则拼接,否则返回空字符
    public static String notEmptyAppend(Object val,String prefix,String suffix){
        return StringUtils.notEmptyAppend(val,prefix,suffix);
    }

    public static String asciiToString(int assic){
       return ""+(char)assic;
    }

    //安全获取,如果为null则用空字符
    public static String selfGet(Object obj,String relationship){
        return selfGet(obj,relationship, StringUtils.NULLSTR);
    }

    //安全获取
    public static String selfGet(Object obj,String relationship,String defaultValue){
        String val = BeanUtils.getRelationshipNameFirstValueString(obj, relationship);
        if (StringUtils.isEmpty(val)) {
            return defaultValue;
        }
        return val;
    }


    public static boolean hasColumn(GenTable genTable, String columnName) {
        if(genTable==null){
            return false;
        }
        return SksTemplateUtils.listHasValue(genTable.getColumns(), OmniCodeGenTableConstants.TABLE_COLUMN_NAME,columnName);
    }

    //首字母小写
    public static String  firstToLowerCase(String str){
        if(StringUtils.isEmpty(str)){
            return StringUtils.NULLSTR;
        }
        String tmp=str;
        tmp = tmp.substring(0, 1).toLowerCase() + tmp.substring(1);
        return tmp;
    }

    //首字母大写
    public static String  firstToUpperCase(String str){
        if(StringUtils.isEmpty(str)){
           return StringUtils.NULLSTR;
        }
        String tmp=str;
        tmp = tmp.substring(0, 1).toUpperCase() + tmp.substring(1);
       return tmp;
    }

    /**
     *
     * @param genTable
     * @param javaField
     * @param javaTypes
     * @return   true:column.javaField 的类型与 javaTypes 中的任何一个相同
     */
    public static boolean javaFieldTypeAnyEquals(GenTable genTable,String javaField,String... javaTypes){
        for (GenTableColumn column : genTable.getColumns()) {
            if (javaField.equals(column.getJavaField())) {
                for (String javaType : javaTypes) {
                    if (javaType.equals(column.getJavaType())) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**
     * 是否为数字类型输入框
     * @param obj
     * @return
     */
    public static String formatRuleMessage(Object obj){
        String htmlType = BeanUtils.getRelationshipNameFirstValueString(obj, "sksFormHtmlType");
        String ruleType = BeanUtils.getRelationshipNameFirstValueString(obj, "sksFormRuleType");
        String message="message: '不能为空',";
        if(htmlType==null){
            htmlType="";
        }
        if (StringUtils.equalsAny(htmlType,"inputFloat","inputInteger","inputPositiveInteger")) {
            if(!StringUtils.equalsAny(ruleType,"defaultRequired")){
                return message;
            }
            return message;
        }
        if(StringUtils.equals(ruleType,"defaultRequired")){
            if (StringUtils.equalsAny(htmlType,"input","textarea","")) {
                return message;
            }
            message="message: '请选择',";
            return message;
        }
        if(StringUtils.equalsAny(ruleType,"numberOnlyGreaterThanZero")){
            message="message: '请选择',";
            return message;
        }
        return message;
    }


    /**
     * 获取 autumnServiceName
     * @param obj
     * @return
     */
    public static String autAppAutumnServiceName(Map<String, Object> autumnGlobal){
        String s = selfGet(autumnGlobal, "autApp.genConfig.autumnServiceName","autumnServiceName");
        return s;
    }

    /**
     *  获取
     * @param autumnGlobal
     * @return
     */
    public static String autAppUniqueId(Map<String, Object> autumnGlobal){
        String s = selfGet(autumnGlobal, "autApp.genConfig.pkJavaField");
        if(StringUtils.isEmpty(s)){
            s = selfGet(autumnGlobal, "autApp.pkJavaField");
        }
        return s;
    }

}
